﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Threading;
using System.Data;

namespace ShowDBPool2
{
    class Program
    {
        static int flag = 0;

        static void Main(string[] args)
        {
            //string strRead = String.Empty;
            //do
            //{

            //} while (String.Equals(strRead.ToLower(), "Y".ToLower()));

            //ThreadPool.QueueUserWorkItem(new WaitCallback(Invoke));


            for (int i = 0; i < 1000; i++)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(Exec));
            }

            Console.ReadLine();

        }

        private static void Exec(Object stateInfo)
        {
            for (int i = 0; i < 10; i++)
            {
                //Invoke(null);
                ThreadPool.QueueUserWorkItem(new WaitCallback(Invoke));
            }
        }

        static void Invoke(Object stateInfo)
        {
            //Initialize the Pool
            DBConnectionSingletion pool = DBConnectionSingletion.Instance;

            //Set the ConnectionString of the DatabaseConnectionPool
            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["ConnString"];
            DBConnectionSingletion.ConnectionString = settings.ConnectionString;
            //Borrow the SqlConnection object from the pool
            SqlConnection conn = pool.BorrowDBConnection();

            DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, "select * from tbwcfsvc;");

            //Console.WriteLine("Execute ds count:" + ds.Tables[0].Rows.Count);
            if (ObjectPool.locked.Count > flag)
            {
                flag = ObjectPool.locked.Count;
            }
            Console.WriteLine("ObjectPool.MAX    :" + flag);
            Console.WriteLine("ObjectPool.Current:" + ObjectPool.locked.Count);

            //Thread.Sleep(2000);

            //Return the Connection to the pool after using it
            pool.ReturnDBConnection(conn);

            //Console.WriteLine("Continue? Y/N");
            //strRead = Console.ReadLine();
        }
    }
}
